<?php
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "EN") {
			$value = "../value/value_en.php";
		} else if($lang == "VN") {
			$value = "../value/value_vn.php";
		}
		else {
			$value = "../value/value_kr.php";
		}
		return $value;
	}
$value = getlang();
include($value);
$listpurchase =$_SESSION['listpurchase'];
include '../operations/connection.php';

$sql="select ac_customer.CustomerName from ac_customer where ac_customer.Activest=1";
$result = mysql_query($sql);
$cus=array();
while($row=mysql_fetch_array($result)){
	$cus[]=array($row['CustomerName']);
}
mysql_close($connection);
#echo $_REQUEST['action'];
if (isset($_REQUEST['action'])){
	if($_REQUEST['action']=='add'){
		$listpurchase =$_SESSION['listpurchase'];
		$co=0;
		foreach ($listpurchase as $purchase){
			$co=$purchase[7];
		}
		$co=$co+1;
		$listpurchase[]=array(0,"","",0,0,0,0,$co,0);
	}else if($_REQUEST['action']=='del'){
		$listpurchase =$_SESSION['listpurchase'];
		$list=array();
		$co=0;
		$temp=0;
		for($i=0;$i<count($listpurchase);$i++){	
			if($listpurchase[$i][8]==1){
				if($temp!=$listpurchase[$i][7])
					$co=$co+1;
				$temp=$listpurchase[$i][7];
			}
			if($listpurchase[$i][8]==0){
					$listpurchase[$i][7]=$listpurchase[$i][7]-$co;
					$list[]=$listpurchase[$i];
				
			}
		}
	$listpurchase=$list;
	}else if($_REQUEST['action']=='check'){
		$listpurchase =$_SESSION['listpurchase'];
		$id=$_REQUEST['id'];
		for($i=0;$i<count($listpurchase);$i++){
			if ($id==$listpurchase[$i][7]){
				$listpurchase[$i][8]=($listpurchase[$i][8]+1)%2;
			}
		}
	}else if($_REQUEST['action']=='addPurchase'){
		$listpurchase =$_SESSION['listpurchase'];
		$id=$_REQUEST['id'];
		$co=0;
		for($i=0;$i<count($listpurchase);$i++){
			if ($listpurchase[$i][7]>$id && $co==0){
				$list[]=array(0,"",$listpurchase[$i-1][2],0,0,0,0,$id,$listpurchase[$i-1][8]);
				$co=1;
			}
			$list[]=$listpurchase[$i];
		}
		if($co==0){
			$list[]=array(0,"",$listpurchase[$i-1][2],0,0,0,0,$id,$listpurchase[$i-1][8]);
		}
		$listpurchase=$list;
	}else if($_REQUEST['action']=='checkPurchase'){
		$listpurchase =$_SESSION['listpurchase'];
		$id=$_REQUEST['id'];
		for($i=0;$i<count($listpurchase);$i++){
			if ($i==($id-1)){
				$listpurchase[$i][0]=($listpurchase[$i][0]+1)%2;
			}
		}
	}else if($_REQUEST['action']=='delPurchase'){
		$listpurchase =$_SESSION['listpurchase'];
		$id=$_REQUEST['id'];
		for($i=0;$i<count($listpurchase);$i++){
			if ($listpurchase[$i][7]==$id){
					if($listpurchase[$i][0]==0){
							$list[]=$listpurchase[$i];
					}
			}else{
				$list[]=$listpurchase[$i];
			}
			
		}
		$listpurchase=$list;
	}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<?php
	$temp=-1;
	$c=0;
	$soct=0;
	foreach ($listpurchase as $purchase){
				
	if($temp!=$purchase[7]){
		$temp=$purchase[7];
		$soct++;
		$c=$purchase[7];
	}
	}
?>
.content_menu_expand{
	position:absolute; 
	right:-5px;
	top:20px;
	z-index:999;
	background-color: #ffffff;
	-moz-border-radius: 7px;
	-webkit-border-radius: 7px;
	color: #000000;
	width:300px;
	max-height:180px;
	overflow:auto;
}

</style>
</head>
<body>
		<?php 
		
			$temp=-1;
			$k=1;
			$c=0;
			$total=0;
			foreach ($listpurchase as $purchase){
				
			if($temp!=$purchase[7]){
				$temp=$purchase[7];
	
			$c=$purchase[7];
			?>
			
		<table id="themcongty" >
			<tr>
				<td style="padding-top: 5px;">
					<div class="btn_cong">
					<?php if($purchase[8]==0){?>
						<input type="checkbox" onclick="checkCompany(<?=$c?>)" />
						<?php }else{?>
						<input type="checkbox" checked="checked" onclick="checkCompany(<?=$c?>)" />
						<?php }?>
					<span>&nbsp;&nbsp;<b><?php echo $l_company_name." :"?></b>&nbsp;</span>
					</div>
				</td>
				<td >
					<div id="customerbox" >
					<div style="float:left">
						<input onkeypress="return focusOnMyInputBox(event,'name<?=$k?>');" onchange="chCompany(this.value,<?=$c?>)" onkeyup="chCompany(this.value,<?=$c?>)" type="text" size="30" value="<?=$purchase[2]?>" name="companyname[]" id="<?echo "inputString".$c;?>"  style="height: 24px; width: 300px; border: 1px solid #c4c0c9;font-size: 12px;" autocomplete="off" />
						<div class="suggestionsBox" id="suggestions" style="display:none;">
							<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
							<div class="suggestionList" id="autoSuggestionsList">
								&nbsp;
							</div>
						</div> 						
					</div>
					<div style=" float:left; position:relative; margin-top: 5px; margin-left: -21px">
						<img alt="" src="images/arrow-down.png" id="<?echo "menu_expand".$c;?>" >
					    <div id="<?echo "content_menu_expand".$c;?>" class="passul content_menu_expand" style="display:none">
					        <ul>
					        <?php 
					    		
								if(count($cus)>0)
								{
									for ($i=0;$i<count($cus);$i++)
									{
										?>
										<li onmouseover="this.style.cursor='pointer'"  onmouseout="this.style.cursor='default'" onclick="clickCustomer('<?=$cus[$i][0]?>','<?=$c?>')" 
											<?php 
												if($i%2!=0){
													?>
													class="bgkhac"
													<?php 
												}
											?>
										> <?php echo $cus[$i][0];?></li>
										<?php 
										
									}
								}
					    	?>
					        </ul>
					    </div>
					</div>
					</div>
				</td>
			</tr>
		</table>
		
		<div id="purchase">
		<table class="tcontent" cellpadding="5" cellspacing="0" id="tblView">
			<tr class="trtit">
				<td width="4%">
					<div style="width: 62px;margin-right: -10px;">
						<input type="button" style="width:24px;height:24px;" value="+" class="btnupbg btnupload" name="plus" id="btnAddMore" onclick="addPurchase(<?=$c?>)" /> 
						<input type="button" style="width:24px;height:24px;" value="-" class="btnupbg btnupload " name="sub" onclick="delPurchase(<?=$c?>)" />
					</div>
				</td>
				<td width="3%"><?php echo $l_no?></td>
				<td width="32%"><?php echo $l_product_item?></td>
				<!--  <td width="30%"><?php //echo $l_company_name?></td> -->
				<td width="8%"><?php echo $l_quantity?></td>
				<td width="8%"><?php echo $l_price?></td>
				<td width="8%"><?php echo $l_price_vat?></td>
				<td width="8%"><?php echo $l_total_amount?></td>
			</tr>
			<?php }
		
				
				
				?>
				<tr class="my_row">
					<?php  if($purchase[0]==0){?>
					<td><input type="checkbox"  onclick="checkPurchase(<?=$k?>)" /></td>
					<?php }else{?>
					<td><input type="checkbox" checked="checked"  onclick="checkPurchase(<?=$k?>)" /></td>
					<?php }?>
					<td><?=$k."-".$c?></td>
					<td><input type="hidden"  name="company[]" value="<?=$purchase[7]?>"/><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpurchase);?>,'0')" onchange="chItemPurchase(this.value,<?=$k?>)" onkeyup="chItemPurchase(this.value,<?=$k?>)" id="<?echo "name".$k?>" name="productitem[]"   style="height: 24px; width: 96%; border: 1px solid #c4c0c9" value="<?=$purchase[1]?>" /></td>
					<td><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpurchase);?>,'1')" onchange="chQuantityPurchase(this.value,<?=$k?>)" onkeyup="chQuantityPurchase(this.value,<?=$k?>)" id="<?echo "txtNumber".$k?>" name="quantity[]" style="height: 24px; width: 96%; border: 1px solid #c4c0c9"	value="<?=$purchase[3]?>" autocomplete="off"  /></td>
					<td><div id='<?echo "price".$k;?>'><input type="text" onkeypress="return enterTable(event,this.id,<?php echo count($listpurchase);?>,'1')" onchange="chPricePurchase(this.value,<?=$k?>)"   onkeyup="chPricePurchase(this.value,<?=$k?>)"  id="<?echo "txtPrice".$k?>" name="price[]"  style="height: 24px; width: 96%; border: 1px solid #c4c0c9"	value="<?=$purchase[4]?>"  autocomplete="off"  /></div></td>
					<td><div id='<?echo "vat".$k;?>'><input onkeypress="return enterTable(event,this.id,<?php echo count($listpurchase);?>,'1')" onchange="chVatPurchase(this.value,<?=$k?>)" onkeyup="chVatPurchase(this.value,<?=$k?>)" id="<?echo "txtVat".$k?>" type="text" name="pricevat[]"    style="height: 24px; width: 96%; border: 1px solid #c4c0c9"  value="<?=$purchase[5]?>" autocomplete="off"  /></div></td>
					<td><div id='<?echo "total".$k;?>'><input type="text" name="totalamount[]" style="height: 24px; width: 96%; border: 1px solid #c4c0c9"  value="<?=number_format($purchase[6])?>" readonly="readonly" /></div></td>
				</tr>
				<?php 
				
				$total=$total+$purchase[6];
				$k++;
				}?>
			
		</table>
		</div>
		<input type="hidden" id="count"  value="<?=$soct?>"/>
		<table class="tcontent" cellpadding="5" cellspacing="0" style="margin-top: 10px">
			<tr>
				<td width="20%" class="col_left"><?php echo $l_total_amount;?></td>
				<td style="text-align: left">
					<div id='all'><input type="text" readonly="readonly"  name="alltotalamount" value="<?=number_format($total)?>" style="height: 24px; width: 30%; border: 1px solid #c4c0c9" /></div>
				</td>
			</tr>
			<tr>
				<td class="col_left"><?php echo $l_comment?></td>
				<td><textarea rows="3" cols="5" name="comment" id="comment"
						style="width: 100%; border: 1px solid #c4c0c9"><?php echo $fcomment?></textarea></td>
			</tr>
		</table>
<?php 
mysql_close($connection);
$_SESSION['listpurchase']=$listpurchase;

?>
</body>
</html>
